#!/usr/bin/env python

# Generate Nginx configuration syntax

# Goals
## General
### Keep it Python 2.4-compatible (I'm looking at you, CentOS)

## Version
### 0.1: generate generic Nginx server {} directive for one host

#import optparse 
#OptionParser = optparse.OptionParser
#make_option = optparse.make_option 

from optparse import OptionParser, make_option
from string import Template 

global VERSION

VERSION = 0.1

def make_server(opts): 
    text = ';\n\n    '.join(opts[1:])
    configuration = Template("""$comment
server {
    $body;
}""")
    return configuration.substitute(comment = opts[0], body = text)
    
def generate_config_for(hostname, options): 
    server_name = 'server_name %s' % hostname + ' '.join([''] + options.alias)
    
    listen = 'listen %s' % options.ip_addr + options.port
    
    if not options.no_log: 
        if not options.access_log: options.access_log = 'access_log /var/log/nginx/%s.acc' % hostname
        if not options.error_log: options.error_log = 'error_log /var/log/nginx/%s.err' % hostname
    
    if options.ip_addr: options.ip_addr += ':'

    if options.cert and options.key: 
        listen = options.ip_addr = options.ssl
    return None

def run_me(): 
    option_list = [
        make_option('--header', 
            help = 'header comment (use quotes)'
        ),
        make_option('-a', '--alias', 
            action = "append", 
            help = 'list aliases or subdomains (www already included)'
        ),
        make_option('-i', '--ip-addr', 
            default = '',
            help = 'ip address'
        ),
        make_option('-p', '--port',
            default = '80',
            help = 'listen port (defaults to 80; SSL is separate)'
        ),
        make_option('-s', '--ssl', 
            default = '443',
            help = 'SSL port (defaults to 443)'
        ), 
        make_option('-c', '--cert', 
            help = 'absolute path to certificate'
        ), 
        make_option('-k', '--key', 
            help = 'absolute path to certificate key'
        ), 
        make_option('--no-logs',
            action = "store_true",
            default = False, 
            help = "no access or error logs" 
        ), 
        make_option('--access-log', 
            help = 'access log location'
        ), 
        make_option('--error-log', 
            help = 'error log location'
        ), 
        make_option('-r', '--root', 
            help = 'document root'
        ), 
        make_option('--cms', 
            help = 'cs-cart, dokuwiki, magento, wordpress, or vtiger, etc.'
        ),
        make_option('-t', '--tech', 
            help = 'php'
        )
    ]
    
    parser = OptionParser(
        option_list = option_list, 
        usage = "%prog [options] hostname",
        version="genginx.py %s" % VERSION
    )
    
    options, args = parser.parse_args()
    
    # Currently just one host name per call is supported 
    hostname = args[0]
    
    if len(args) < 1: 
        parser.print_help()
    else: 
        configuration = generate_config_for(hostname, options)
        
if __name__ == '__main__': 
    run_me()